package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.SystemLog;
import cn.wolfcode.crm.service.ISystemLogService;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class SystemLogUtil {
    @Autowired
    private ISystemLogService systemLogService;

    public void writLog(JoinPoint joinPoint) {
        if (joinPoint.getTarget() instanceof ISystemLogService) {
            return;
        }
        SystemLog systemLog = new SystemLog();
        systemLog.setOpTime(new Date());
        systemLog.setOpUser(UserContext.getCurrentUser());
        //从请求当中获取请求的ip地址
        systemLog.setOpIp(UserContext.getRequest().getRemoteAddr());
        //获取当前事务控制的方法所在类的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取当前事务控制的方法的名称
        String methodName = joinPoint.getSignature().getName();
        systemLog.setFunction(className + ":" + methodName);
        //将多个参数已json的格式显示出来
        int length = JSON.toJSONString(joinPoint.getArgs()).length();
        if (length <= 20000) {
            systemLog.setParams(JSON.toJSONString(joinPoint.getArgs()));
        }
        systemLogService.insert(systemLog);
    }
}
